﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Business.PUBC;
using Mang24k99.Model.PUBC;
using Mang24k99.Model.COMM;

namespace Mang24k99.Web.PUBC
{
    public partial class BoardItems : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["no"] != null)
                {
                    string cycno = Request.QueryString["no"].ToString();
                    MKtPubcCyc cyc = BKtPubcCyc.Find(Convert.ToInt32(cycno));
                    ddlBoardType.SelectedValue = cyc.MKtPubcProduct.BoardNO.ToString();
                    ddlBoardCyc.SelectedValue = cyc.CycNO.ToString();
                }
                this.Panel2.Visible = false;
                BindBoardType();
                BindBoardCyc();
                BindGridView();
            }
        }
        /// <summary>
        /// 绑定调查表类型
        /// </summary>
        public void BindBoardType()
        {
            MKtPubcProduct[] productlist = (MKtPubcProduct[])BKtPubcProduct.SelectByCycNum();
            this.ddlBoardType.DataSource = productlist;
            this.ddlBoardType.DataTextField = "BoardAlias";
            this.ddlBoardType.DataValueField = "BoardNO";
            this.ddlBoardType.DataBind();
        }
        /// <summary>
        /// 绑定周期
        /// </summary>
        public void BindBoardCyc()
        {
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            MKtPubcCyc[] cyclist = (MKtPubcCyc[])BKtPubcCyc.SelectByBoardNO(boardno);
            this.ddlBoardCyc.DataSource = cyclist;
            this.ddlBoardCyc.DataTextField = "CycAlias";
            this.ddlBoardCyc.DataValueField = "CycNO";
            this.ddlBoardCyc.DataBind();
        }
        /// <summary>
        /// 绑定条目数
        /// </summary>
        public void BindGridView()
        {
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            int cycno = Convert.ToInt32(ddlBoardCyc.SelectedValue);
            MKtPubcItems[] itemslist = (MKtPubcItems[])BKtPubcItems.SelectItems(boardno, cycno);
            this.grdList.DataSource = itemslist;
            this.grdList.DataBind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            this.Panel3.Visible = false;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidatePage()==false)
            {
                return;
            }
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            int cycno = Convert.ToInt32(ddlBoardCyc.SelectedValue);

            if (btnSave.Text=="新增")
            {
                if (NameValidate()==false)
                {
                    return;
                }
                if (AliasValidate()==false)
                {
                    return;
                }
                MKtPubcItems item = new MKtPubcItems();
                item.ItemNO = (int)MKtPubcItems.GetMaxCycItemsNO() + 1;
                item.ItemName = txtItemName.Text.Trim();
                item.ItemAlias = txtItemAlias.Text.Trim();
                item.PowerRtio = Convert.ToInt32(txtPowerRtio.Text.Trim());
                MKtPubcProduct product = BKtPubcProduct.Find(boardno);
                item.MKtPubcProduct = product;
                MKtPubcCyc cyc = BKtPubcCyc.Find(cycno);
                cyc.TargetNum += 1;
                item.MKtPubcCyc = cyc;
                item.IsValidate = 1;
                item.InputTime = DateTime.Now;
                try
                {
                    cyc.Update();
                    item.Create();
                    string strMsg = "新增成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
            if (btnSave.Text=="更新")
            {
                int itemno = Convert.ToInt32(txtItemNO.Text);
                MKtPubcItems item = BKtPubcItems.Find(itemno);
                if (txtItemName.Text.Trim()!=item.ItemName)
                {
                    if (NameValidate()==false)
                    {
                        return;
                    }
                }
                if (txtItemAlias.Text.Trim()!=item.ItemAlias)
                {
                    if (AliasValidate()==false)
                    {
                        return;
                    }
                }
                item.ItemName = txtItemName.Text.Trim();
                item.ItemAlias = txtItemAlias.Text.Trim();
                item.PowerRtio = Convert.ToInt32(txtPowerRtio.Text.Trim());
                item.ModifyTime = DateTime.Now;
                try
                {
                    item.Update();
                    string strMsg = "更新成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (btnSave.Text == "新增")
            {
                ClearPage();
            }
            else
            {
                int itemno = Convert.ToInt32(txtItemNO.Text);
                MKtPubcItems item = BKtPubcItems.Find(itemno);
                txtItemName.Text = item.ItemName;
                txtItemAlias.Text = item.ItemAlias;
                txtPowerRtio.Text = item.PowerRtio.ToString();
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
            ClearPage();
        }
        public void ClearPage()
        {
            txtItemAlias.Text = "";
            txtItemName.Text = "";
            txtItemNO.Text = "";
            txtPowerRtio.Text = "";
            ddlBoardCyc.Enabled = true;
            ddlBoardType.Enabled = true;
            btnSave.Text = "新增";
        }
        protected void ddlBoardType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindBoardCyc();
            BindGridView();
        }

        protected void ddlBoardCyc_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }
        /// <summary>
        /// 验证显示名称是否重复
        /// </summary>
        /// <returns></returns>
        public bool NameValidate()
        {
            string itemname = txtItemName.Text.Trim();
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            int cycno = Convert.ToInt32(ddlBoardCyc.SelectedValue);
            MKtPubcItems[] items = (MKtPubcItems[])BKtPubcItems.NameValidate(boardno, cycno, itemname);
            if (items.Length>0)
            {
                string strMsg = "已经有该条目显示名称，请更换名称！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 验证条目简称是否重复
        /// </summary>
        /// <returns></returns>
        public bool AliasValidate()
        {
            string itemalias = txtItemAlias.Text.Trim();
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            int cycno = Convert.ToInt32(ddlBoardCyc.SelectedValue);
            MKtPubcItems[] items = (MKtPubcItems[])BKtPubcItems.AliasValidate(boardno, cycno, itemalias);
            if (items.Length > 0)
            {
                string strMsg = "已经有该条目简称，请更换名称！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        public bool ValidatePage()
        {
            if (txtItemName.Text.Trim()=="")
            {
                string strMsg = "条目显示名称不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (txtItemAlias.Text.Trim()=="")
            {
                string strMsg = "条目简称不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (txtPowerRtio.Text.Trim()=="")
            {
                string strMsg = "权重不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (Common.PageValidate.IsNumber(txtPowerRtio.Text.Trim())==false)
            {
                string strMsg = "权重输入格式不对，请输入正整数！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (Convert.ToInt32(txtPowerRtio.Text.Trim())==0)
            {
                string strMsg = "权重不能为0，请输入正整数！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int itemno = Convert.ToInt32(e.CommandArgument.ToString());
            MKtPubcItems item = BKtPubcItems.Find(itemno);
            MKtPubcCyc cyc = item.MKtPubcCyc;
            if (e.CommandName=="Modi")
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                this.Panel3.Visible = false;
                txtItemName.Text = item.ItemName;
                txtItemNO.Text = item.ItemNO.ToString();
                txtItemAlias.Text = item.ItemAlias;
                txtPowerRtio.Text = item.PowerRtio.ToString();
                ddlBoardCyc.Enabled = false;
                ddlBoardType.Enabled = false;
                btnSave.Text = "更新";
            }
            if (e.CommandName=="Del")
            {
                try
                {
                    //删除一个条目则周期表条目数量减一
                    cyc.TargetNum -= 1;
                    cyc.Update();
                    item.Delete();
                    string strMsg = "删除成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }
    }
}
